﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using TheOutfield.UmbExt.UniversalMediaPicker.Controls;

namespace Our.Umbraco.UniversalMediaPicker.Providers.TwitPic
{
	public class TwitPicConfigControl : AbstractConfigControl
	{
		private TwitPicConfig config;
		private TextBox username = new TextBox();

		public override string Value
		{
			get
			{
				return new TwitPicConfig()
				{
					Username = this.username.Text
				}
				.SerializeToJson();
			}
		}

		public TwitPicConfigControl(string config)
		{
			this.config = config.DeserializeJsonTo<TwitPicConfig>();
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			this.EnsureChildControls();
		}

		protected override void OnLoad(EventArgs e)
		{
			if (!this.Page.IsPostBack)
			{
				this.username.Text = this.config.Username;
			}

			base.OnLoad(e);
		}

		protected override void CreateChildControls()
		{
			this.username.ID = "TwitPicUsername";
			this.username.CssClass = "guiInputText guiInputStandardSize";

			this.Controls.Add(this.username);
		}

		protected override void RenderContents(HtmlTextWriter writer)
		{
			writer.AddFormRow("Username:", "Please enter your TwitPic username.", this.username);
		}
	}
}
